home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / archvrs / msdos / unshar / dos_c.c next >
Encoding:
C/C++ Source or Header  |  1986-09-25  |  6.6 KB  |  302 lines

  1. /*
  2.  *    DOS_C.C    -    is a Un*x compatibility module for Desmet C-Ware
  3.  *        C Compiler.  This module comes in two parts.  All Assembly
  4.  *        Language code is isolated in DOS_A.A - the necessary 
  5.  *        structure invokations and defines are found in DOS.H
  6.  *        This if far from being either complete or faithful.  The
  7.  *        machinations of making MsDos look like Un*x at the program
  8.  *        level are far from trivial (far from possible).  Such is
  9.  *        life in 8088 land.
  10.  *
  11.  *                      ---   24 Sep '86   ---
  12.  *                    ---   John Birchfield   ---
  13.  */
  14.  
  15. # include "dos.h"
  16.  
  17. char clock [20];
  18.  
  19.  
  20.  
  21.  
  22. /*    STAT ()    -    a very miniscule implementation of stat.  In this
  23.  *        implementation.  the only information returned by stat is
  24.  *        the file's date & time of last update, the file's size, 
  25.  *        and the attributes of the file.  All this information is
  26.  *        returned in struct stat which is defined in DOS.H - If
  27.  *        stat succeeds a 0 is returned, otherwise a 1
  28.  *
  29.  *    Usage if (stat (filename, &statbuf)==0)
  30.  *            SUCCESS;
  31.  *
  32.  */
  33.  
  34. int stat (filename, statbuf)
  35. char *filename;
  36. struct stat *statbuf;
  37. {
  38.     struct direct *dirp, *opendir ();
  39.     int fp;
  40.  
  41.     if ((dirp=opendir (filename))==(struct direct *) NULL)
  42.         return -1;
  43.     statbuf->st_mode = dirp->d_mode;
  44.     statbuf->st_time = dirp->d_time;
  45.     statbuf->st_date = dirp->d_date;
  46.     statbuf->st_size =  (dirp->d_mode & ST_DIR)?0L:dirp->d_size;
  47.     free (dirp);
  48.     return 0;
  49. }
  50.  
  51.  
  52.  
  53.  
  54. /*
  55.  *    OPENDIR ()    -    opendir () is not a documented Un*x function
  56.  *        but for implementing some of the Un*x style directory
  57.  *        reads it is handy  along with readdir ().  The primary
  58.  *        logical difference is that Un*x only performs an OPEN
  59.  *        and doesn't return any immediate information until the
  60.  *        first readdir ().  All information is returned in
  61.  *        struct direct { again look in DOS.H }.  If successful, 
  62.  *        opendir returns a pointer to the structure containing
  63.  *        information necessary to perform subsequent readdir ()
  64.  *        calls.  Because the dta {Disk Transfer Address } is
  65.  *        set with each call it's possible to have multiple
  66.  *        directories open concurrently.
  67.  *
  68.  *    Usage:    if (dirp=opendir ("mask"))
  69.  *            SUCCESS;
  70.  *    Note:    A call of opendir ("*.*") would allow searching the entire
  71.  *        current directory.  All file attributes are set so that all
  72.  *        files {Hidden - System - Directory} will be returned.  The
  73.  *        Volume Label will NOT be returned by this call.
  74.  */
  75.  
  76. struct direct *opendir (mask)
  77. char *mask;
  78. {
  79.     struct direct *tmp;
  80.  
  81.     if ((tmp = malloc (sizeof (struct direct)))==(struct direct *) NULL)
  82.         return ((struct direct *) NULL);
  83.     set_dta (tmp);
  84.     if (a_open_dir (mask)==0) {
  85.         free (tmp);
  86.         return ((struct direct *) NULL);
  87.     }
  88.     return tmp;
  89. }
  90.  
  91.  
  92.  
  93.  
  94. /*    READDIR ()    -    readdir () is the continuation call 
  95.  *        corresponding to opendir ().  Read above for more
  96.  *        complete information.  Mainly you use the pointer to the
  97.  *        direct structure to continue what was started above.
  98.  *        readdir () returns NULL upon failure.  Use closedir ()
  99.  *        or free () to free up the memory consumed by *dirp.
  100.  
  101.  *    Usage:    while (dirp=readdir (dirp) {
  102.  *            process directory ...
  103.  *        }
  104.  */
  105.  
  106. struct direct *readdir (dp)
  107. struct direct *dp;
  108. {
  109.     set_dta (dp);
  110.     if (a_read_dir ()==0)
  111.         return ((struct direct *) NULL);
  112.     return dp;
  113. }
  114.     
  115.  
  116.  
  117.  
  118. /*
  119.  *    CLOSEDIR ()    -    What is there to say - it could be done
  120.  *        with a macro ...
  121.  */
  122.  
  123. void closedir (dp)
  124. struct direct *dp;
  125. {
  126.     free (dp);
  127. }
  128.  
  129.  
  130.  
  131.  
  132. /*
  133.  *    ISDIR ()    -    isdir () returns TRUE if the file is
  134.  *        a directory.  Uses opendir () to get the info.
  135.  *
  136.  *    Usage: if isdir ("mydir")
  137.  *            chdir ("mydir");
  138.  */
  139.  
  140. int isdir (name)
  141. char *name;
  142. {
  143.     struct direct *tmp;
  144.     int rval = FALSE;
  145.  
  146.     if (tmp=opendir (name)) {
  147.         if (tmp->d_mode & ST_DIR)
  148.             rval = TRUE;
  149.         free (tmp);
  150.     }
  151.     return rval;
  152. }
  153.  
  154.  
  155.  
  156.  
  157. /*
  158.  *    CTIME ()    -    ctime is a sort of semi compatible Un*x
  159.  *        call.  If I was gonna do it write, I'd have translated
  160.  *        the date & time completely.   As it is the date & time
  161.  *        are returned in the char buffer passed in the following
  162.  *        form.
  163.  *            "yy-mm-dd  hh-mm-ss"
  164.  *        Which requires that buf be 19 characters in length.
  165.  *
  166.  *    Usage:    ctime (buf);
  167.  */
  168.  
  169. char *ctime (s)
  170. char **s;
  171. {
  172.     char t_buf [10];
  173.  
  174. # ifdef DEBUG
  175.     db_out("Ctime ...\n");
  176. # endif
  177.  
  178.  
  179.     dates (t_buf);
  180.     strcpy (*s, t_buf);
  181.     strcat (*s, "  ");
  182.     times (t_buf);
  183.     strcat (*s, t_buf);
  184. }
  185.  
  186.  
  187.  
  188. /*
  189.  *    GETENV ()    -    looks for the environment variable
  190.  *        passed to it.  If found it returns a pointer to the
  191.  *        variable's value - if not NULL.
  192.  *
  193.  *    Usage:    if (cp = get_env ("PATH"))
  194.  *             printf ("Found PATH = '%s'\n", cp);
  195.  */
  196.  
  197. char *getenv (pat)
  198. char        *pat;
  199. {
  200.     extern unsigned _PCB;
  201.     unsigned seg   = 0,
  202.          offst = 0;
  203.     char     *ch;
  204.     int     i, length, copylen;
  205.     char     tmp [256],
  206.          *str;
  207.     length = strlen (pat);
  208.     copylen = (*(pat+length-1)=='=')?length:length+1;
  209.     *str = '\0';
  210.     seg = (_peek (0x2d, _PCB)<<8) + _peek (0x2c, _PCB);
  211.  
  212.  
  213.     while (_peek (offst, seg) != 0) {
  214.         i = 0;
  215.         ch = tmp; *ch = '\0';
  216.         while (*ch = _peek (offst++, seg))
  217.             if (++i < 100)
  218.                 ch++;
  219.         *ch = '\0';
  220.         if (strncmp(pat, tmp, length) == 0) {
  221.             length = strlen (tmp+copylen) +1;
  222.             str = malloc (length);
  223.             strcpy (str, tmp + copylen);
  224.             return (str);
  225.         }
  226.     }
  227.     return (NULL);
  228. }
  229.  
  230.  
  231.  
  232. /*
  233.  *    RAWMODE ()    -    rawmode () uses ioctl ()  { in dos_a.a }
  234.  *        to turn off ^C checking on  {stdin stdout & stderr }
  235.  *        When this is done {along with avoiding the putchar () and
  236.  *        getchar () function calls} one stands a minor chance of
  237.  *        not having TABS expanded to SPACES and ETX's replaced with
  238.  *        ^C's.  Also some ^Z problems are alleiated.
  239.  *
  240.  *    Note:    For stdio ALL THREE HANDLES MUST BE SET FOR IT TO WORK.
  241.  *        Also note that this call will ONLY work with CHARACTER
  242.  *        DEVICE files.  When this mode is set it will remain in effect
  243.  *        until programatically changed or a SYSTEM BOOT occurs.
  244.  *
  245.  *    Usage:    rawmode (stdin); rawmode (stdout); rawmode (stderr);
  246.  */
  247.  
  248. int rawmode (fp)
  249. int fp;
  250. {
  251.     struct ioctlbuf iob;
  252.     iob.cmd = IOCTL_GET;
  253.     iob.handle = fp;
  254.     ioctl (&iob);
  255.     iob.cmd = IOCTL_SET;
  256.     iob.devdata &= 0xFF;
  257.     iob.devdata |= RAW_MODE;
  258.     return (ioctl (&iob));
  259. }
  260.  
  261.  
  262.  
  263.  
  264. /*
  265.  *    NORMODE ()    -    same as above...
  266.  *
  267.  *    Usage:  normode (stdin); etc...
  268.  */
  269.  
  270. int normode (fp)
  271. int fp;
  272. {
  273.     struct ioctlbuf iob;
  274.     iob.cmd = IOCTL_GET;
  275.     iob.handle = fp;
  276.     ioctl (&iob);
  277.     iob.cmd = IOCTL_SET;
  278.     iob.devdata &= 0xFF;
  279.     iob.devdata &= COOKED_MODE;
  280.     return (ioctl (&iob));
  281. }
  282.  
  283.  
  284.  
  285.  
  286. /*
  287.  *    _EXIT ()    -    _exit () lets you exit but allows you to
  288.  *        turn off RAW_MODE on stdio before saying goodbye to the
  289.  *        world.
  290.  *
  291.  *    Usage:    _exit (5);
  292.  */
  293.  
  294. int _exit (n)
  295. int n;
  296. {
  297.     normode (0);
  298.     normode (1);
  299.     normode (2);
  300.     exit (n);
  301. }
  302.